		TITLE	FANCY - Copyright (c) 1994 SLR Systems, Inc

		INCLUDE	MACROS
		INCLUDE	IO_STRUC

		PUBLIC	FANCY_OPEN_INPUT,DELETE_PHANTOM_PATH


		.DATA

		EXTERNDEF	OBJ_DEVICE:DWORD,CURN_FILE_LIST_GINDEX:DWORD

		EXTERNDEF	_FILE_LIST_GARRAY:STD_PTR_S


		.CODE	ROOT_TEXT

		EXTERNDEF	_open_input:proc,_move_ecxpath_eax:PROC
		externdef	_delete_phantom_path:proc

;MYI_STRUCT *FANCY_OPEN_INPUT(NFN_STRUCT *EAX, FILE_LISTS *ECX);
; returns NULL on error

;		public	_fancy_open_input
;_fancy_open_input:
		mov	ECX,8[ESP]
		mov	EAX,4[ESP]

FANCY_OPEN_INPUT	PROC
		;
		;EAX IS NFN_STRUCT CONTAINING RAW FILENAME
		;ECX IS LIST OF PATHS...
		;
		PUSHM	ECX,EAX

		mov	ECX,offset OBJ_DEVICE
		push	ECX
		push	EAX
		call	_open_input
		add	ESP,8

		POPM	EDX,ECX

		test	EAX,EAX
		jnz	FANCY_FIX
		MOV	EAX,OBJ_DEVICE
		clc
		RET


FANCY_FIX:
		;
		;WELL, IT WOULD HAVE BEEN NICE...
		;
		PUSH	ESI
		MOV	ESI,EDX
		ASSUME	ESI:PTR NFN_STRUCT

		PUSH	EDI
		MOV	EDI,ECX

		MOV	AL,[ESI].NFN_FLAGS
		PUSH	EBX

		TEST	AL,MASK NFN_PATH_SPECIFIED
		JNZ	FANCY_FAIL

		MOV	EDI,[EDI].FILE_LISTS.FILE_FIRST_GINDEX
		CONVERT	EDI,EDI,_FILE_LIST_GARRAY
		ASSUME	EDI:PTR FILE_LIST_STRUCT

		MOV	EDI,[EDI].FILE_LIST_NEXT_GINDEX
		JMP	TEST_PATH

PATH_LOOP:
		MOV	EBX,EDI
		CONVERT	EDI,EDI,_FILE_LIST_GARRAY
		MOV	AL,[EDI].FILE_LIST_NFN.NFN_FLAGS

		LEA	ECX,[EDI].FILE_LIST_NFN
		MOV	EDI,[EDI].FILE_LIST_NEXT_GINDEX

		TEST	AL,MASK NFN_PATH_SPECIFIED	;SKIP 'NUL'
		JZ	NEXT_PATH

		MOV	EAX,ESI
		;
		;NEED TO MOVE JUST PATH from ECX to EAX
		;
		push	ECX
		push	EAX
		CALL	_move_ecxpath_eax		;MOVE JUST PATHNAME
		add	ESP, 8

		mov	ECX,offset OBJ_DEVICE
		push	ECX
		push	EAX
		call	_open_input
		add	ESP,8

		test	EAX,EAX
		mov	EAX,OBJ_DEVICE
		jz	FANCY_SUCCESS
		;
		;TRY NEXT PATH
		;
NEXT_PATH:
TEST_PATH:
		TEST	EDI,EDI
		JNZ	PATH_LOOP
FANCY_FAIL:
		push	ESI
		call	_delete_phantom_path
		add	ESP,4

		CMP	ESP,-1
		mov	EAX,0
		JMP	FS_7

FANCY_SUCCESS:
		MOV	EDX,CURN_FILE_LIST_GINDEX
		MOV	OBJ_DEVICE,EAX

		TEST	EDX,EDX
		JZ	FS_6

		CONVERT	EDX,EDX,_FILE_LIST_GARRAY
		ASSUME	EDX:PTR FILE_LIST_STRUCT

		MOV	[EDX].FILE_LIST_PATH_GINDEX,EBX
FS_6:
		CMP	EAX,EAX
FS_7:
		POPM	EBX,EDI,ESI

		RET

FANCY_OPEN_INPUT	ENDP


DELETE_PHANTOM_PATH	PROC
		;
		;
		;
		TEST	[ESI].NFN_FLAGS,MASK NFN_PATH_SPECIFIED
		JNZ	L9$

		MOV	EAX,ESI
		MOV	ECX,OFF NUL_PATH

		push	ECX
		push	EAX
		CALL	_move_ecxpath_eax
		add	ESP,8
L9$:
		RET

DELETE_PHANTOM_PATH	ENDP


		.CONST

NUL_PATH	NFN_STRUCT	<>

		END

